import random
import time
from datetime import datetime


def click(x, dur: int = 2):
    now = datetime.now()
    print(f"click: {x}")
    print(datetime.now() - now)


def sleep(duration):
    time.sleep(duration)



# 随机休眠 动态参数，参数可以是 list [min,max] 也可以是dur：int 也可以是 dict {"min":1,"max":2}  最后打印出执行时间 

def random_sleep(sleep_duration):
    """
    根据给定的睡眠持续时间参数随机休眠。
    参数可以是整数（固定睡眠时间）、列表[min, max] 或字典{"min": min_val, "max": max_val}。
    """
    if isinstance(sleep_duration, int):
        time.sleep(sleep_duration)
    elif isinstance(sleep_duration, list) and len(sleep_duration) == 2:
        time.sleep(random.uniform(sleep_duration[0], sleep_duration[1]))
    elif isinstance(sleep_duration, dict) and "min" in sleep_duration and "max" in sleep_duration:
        time.sleep(random.uniform(sleep_duration["min"], sleep_duration["max"]))
    else:
        raise ValueError("Invalid sleep duration format")
    
def execute_with_random_sleep(sleep_duration):
    start_time = time.time()
    random_sleep(sleep_duration)
    end_time = time.time()
    print(f"Execution took {end_time - start_time:.2f} seconds.")